package com.gzz.demo.crypt;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import java.util.Base64;

public class Degist {
    public static void main(String[] args) {
        Degist degist = new Degist();
        try {
            //给渠道分配的秘钥
            String pswdWeb = "BBDmwTjBsF7IwTIyGWt1bmFn";

            String originWeb = "channel1,order001,20121210231000,merchant01,13600000000,,0,10,";
            String digestWeb = degist.getDigest(originWeb, pswdWeb);
            System.out.println("指纹:" + digestWeb);
            //支付结果后台通知指纹
            //degist.serverNotifyDegister();
            //支付结果页面通知指纹
            //degist.webNotifyDegister();
            //订单页面提交指纹
            degist.orderDegister();
        } catch (Exception exp) {

        }
    }


    /**
     * 支付结果通知（后台）指纹算法
     *
     * @throws Exception
     */
    public void serverNotifyDegister() throws Exception {
        //支付单号
        String paymentorderId = "789456321";
        //订单号
        String orderid = "123456789";
        //订单积分数量
        String points = "0";
        //订单金额（单位分）
        String amount = "10";
        //支付机构原始响应吗
        String OriginPayErrorCode = "zh234567";
        //订单状态
        String paymentstatus = "2";


//                String alg = "DESede" ;
        //给渠道分配的秘钥
        String pswd = "BBDmwTjBsF7IwTIyGWt1bmFn";


        String origin = paymentorderId + ","

                + orderid + ","
                + points + ","
                + amount + ","
                + OriginPayErrorCode + ","
                + paymentstatus + ",";
        String degist = this.getDigest(origin, pswd);

        System.out.println(degist);
    }

    /**
     * 支付结果通知（页面）指纹算法
     *
     * @throws Exception
     */
    public void webNotifyDegister() throws Exception {
        //支付单号
        String paymentorderId = "789456321";
        //订单号
        String orderid = "20130614121234";
        //订单时间
        String ordertime = "20130614121234";
        //商户标识
        String merchant = "merchant";
        //订单积分数量
        String points = "0";
        //订单金额（单位分）
        String amount = "10";

        //订单状态
        String paymentstatus = "2";
        //结算日期
        String acDate = "20130615";

//                String alg = "DESede" ;
        //给渠道分配的秘钥
        String pswd = "BBDmwTjBsF7IwTIyGWt1bmFn";


        String origin = paymentorderId + ","
                + orderid + ","
                + ordertime + ","
                + merchant + ","
                + points + ","
                + amount + ","
                + paymentstatus + ","
                + acDate + ",";

        String degist = this.getDigest(origin, pswd);

        System.out.println(degist);
    }

    /**
     * 订单一次提交的指纹算法
     *
     * @throws Exception
     */
    public void orderDegister() throws Exception {
        //渠道编码
        String c_idWeb = "01";
        //订单号
        String o_idWeb = "20130614121234";
        //订单时间
        String o_timeWeb = "20130614121234";
        //商户
        String merchant = "010100GD001";
        //订单用户
        String user = "13926405877";
        //订单支付用户
//                String puser= null ;
        //订单积分数量
        int point = 0;
        //订单金额
        int amount = 10;
        //给渠道分配的秘钥
        String pswdWeb = "BBDmwTjBsF7IwTIyGWt1bmFn";


        String originWeb = c_idWeb + ","
                + o_idWeb + ","
                + o_timeWeb + ","
                + merchant + ","
                + user + ","
                + ","
                + point + ","
                + amount + ",";
        System.out.println("待检测：" + originWeb);
        String digestWeb = getDigest(originWeb, pswdWeb);
        System.out.println("订单一次提交的指纹:" + digestWeb);
    }

    /**
     * digest
     *
     * @param origin
     * @param pswd
     * @return
     * @throws Exception
     */
    private String getDigest(String origin, String pswd) throws Exception {
        String alg = "DESede";
        DESedeKeySpec dks = new DESedeKeySpec(pswd.getBytes());

        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(alg);

        SecretKey secretKey = keyFactory.generateSecret(dks);


        Cipher cipher = Cipher.getInstance(alg);

        cipher.init(Cipher.ENCRYPT_MODE, secretKey);

        byte[] after = cipher.doFinal(origin.getBytes());

        //String digest = new String( Base64.encodeBase64( DigestUtils.md5(after) ) ) ;
        String digest = new String(Base64.getEncoder().encode(after));
        return digest;
    }


}
